<?php
namespace App\Models;

use Illuminate\Support\Facades\DB;

class ClientUserHouse extends CommonModel {
	protected $table = 'client_user_house';
	
	
	/**
	 * @return $this
	 * 房屋详情
	 */
	public function detail(){
		$house_id = $this->getAttribute('house_id');

		$details = $this->parent($house_id)->reverse()->values();
		$this->setAttribute('build_area',$details->first()->build_area);
		$this->setAttribute('use_area',$details->first()->use_area);
		$this->setAttribute('name_long',$details->pluck('name')->implode(''));
		$this->setAttribute('details',$details);
		
		// 车位信息
		$this->setAttribute('car',HouseDistrictCar::select('id','name','remarks','created_at')->where(['house_id'=>$house_id])->get());
		// 查询待缴费信息

		$this->setAttribute('costs', HouseDistrictCosts::where(['house_id'=>$house_id])->unpaid()->sum('amount'));

		return $this;
	}
	public function getNameLong($house_id){
          $details = $this->parent($house_id)->reverse()->values();
          return $details->pluck('name')->implode('');
     }
	private function parent($parent_id){
		$return = collect([]);
		$data = HouseDistrict::find($parent_id);
		if($data){
			$parent = $this->parent($data->parent_id);
			$return->push($data);
			$return = $return->merge($parent);
		}
		return $return;
	}
}